//
//  IdeaViewController.m
//  LiangYa_BObject
//
//  Created by dllo on 15/10/28.
//  Copyright © 2015年 YuHao. All rights reserved.
//

#import "IdeaViewController.h"
#import "AppDelegate.h"
#import "IdeaCollectionViewCell.h"
#import "IdeaData.h"
#import "IdeaWaterflowLayout.h"
#import "IdeaDetallViewController.h"

@interface IdeaViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,HMWaterflowLayoutDelegate>{
    MBProgressHUD *HUD;
}
@property (nonatomic, weak) UICollectionView *collectionView;
@property (nonatomic, strong) NSMutableArray *creativeArr;
@property (nonatomic, assign) NSInteger i;


@end

@implementation IdeaViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewWillAppear:(BOOL)animated
{
    AppDelegate *appD = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appD.leftSlideVC setPanEnabled:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.title = @"推荐";
    self.view.backgroundColor = [UIColor clearColor];
    
    UIButton *menuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    menuBtn.frame = CGRectMake(0, 0, 20, 18);
    [menuBtn setBackgroundImage:[UIImage imageNamed:@"drawer"] forState:UIControlStateNormal];
    [menuBtn addTarget:self action:@selector(leftAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuBtn];

    self.i = 1;
    [self handleData];
    IdeaWaterflowLayout *layout = [[IdeaWaterflowLayout alloc] init];
    layout.delegate = self;
    self.creativeArr = [NSMutableArray array];
    
    UICollectionView * collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, Screenheight) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.dataSource = self;
    collectionView.delegate = self;
    collectionView.showsVerticalScrollIndicator = NO;
    [collectionView registerClass:[IdeaCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collectionView.backgroundColor = [UIColor clearColor];
    self.collectionView = collectionView;
    [self.view addSubview:collectionView];
    
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(handleData)];
    self.collectionView.header = header;

    
#pragma mark 上拉加载
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadMoreData];
        
    }];
    self.collectionView.footer = footer;

    HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.labelText = @"数据加载中...";
    
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(nightAction:) name:@"night" object:nil];
}

- (void)nightAction:(NSNotification *)sender
{
    self.view.backgroundColor = [sender.userInfo objectForKey:@"backcolor"];
}

- (void)leftAction
{
    AppDelegate *appD = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if (appD.leftSlideVC.closed) {
        [appD.leftSlideVC openLeftView];
    }else{
        [appD.leftSlideVC closeLeftView];
    }
    
}

#pragma mark 瀑布流协议方法
- (CGFloat)waterflowLayout:(IdeaWaterflowLayout *)waterflowLayout heightForWidth:(CGFloat)width atIndexPath:(NSIndexPath *)indexPath
{
    IdeaData *creative = self.creativeArr[indexPath.item];
    return creative.height / creative.width * width;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.creativeArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    IdeaCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    IdeaData *creative = [self.creativeArr objectAtIndex:indexPath.item];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:creative.pic] placeholderImage:[UIImage imageNamed:@"baikejiaju.png"]];

    cell.myLabel.text = creative.albumName;
    return cell;
}

#pragma mark 网络加载
- (void)handleData
{

    NSString *str = @"http://editors.zuiqushi.com/album_json.php?version=6.11&package=com.aslibra.wt.jiaju&ipad=0&cat=(null)&page=1";
    [AFRequdstAndLoad dataURLStr:str datahand:^(id object) {
        self.creativeArr = [NSMutableArray array];

        if (object) {
            [HUD removeFromSuperview];
            [self.collectionView.header endRefreshing];
        }
        NSDictionary *dic1 = [object objectForKey:@"data"];
        NSArray *arr = [dic1 objectForKey:@"data"];
        for (NSDictionary *dic2 in arr) {
            IdeaData *ideaData = [[IdeaData alloc] init];
            [ideaData setValuesForKeysWithDictionary:dic2];
            [self.creativeArr addObject:ideaData];
        }
        [self.collectionView reloadData];
        
    }];
}
#pragma mark 上拉加载数据
- (void)loadMoreData
{
    self.i++;
    NSNumber *a = [NSNumber numberWithInteger:self.i];
    NSString *str = [@"http://editors.zuiqushi.com/album_json.php?version=6.11&package=com.aslibra.wt.jiaju&ipad=0&cat=(null)&page=" stringByAppendingFormat:@"%@",a];
        [AFRequdstAndLoad dataURLStr:str datahand:^(id object) {
            if (object) {
                [HUD removeFromSuperview];
                [self.collectionView.footer endRefreshing];
            }
            NSDictionary *dic1 = [object objectForKey:@"data"];
            NSArray *arr = [dic1 objectForKey:@"data"];
            for (NSDictionary *dic2 in arr) {
                IdeaData *ideaData = [[IdeaData alloc] init];
                [ideaData setValuesForKeysWithDictionary:dic2];
                [self.creativeArr addObject:ideaData];
            }
            [self.collectionView reloadData];
    
        }];
}

#pragma mark collectionViewCell 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    IdeaDetallViewController *ideaDetaVC = [[IdeaDetallViewController alloc] init];
    ideaDetaVC.boo = YES;
    ideaDetaVC.ideaData = [self.creativeArr objectAtIndex:indexPath.row];
    AppDelegate *appD = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    [appD.leftSlideVC setPanEnabled:NO];
    [self.navigationController pushViewController:ideaDetaVC animated:YES];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
